<!--
  * 座位信息表
  *
  * @Author:    dragon
  * @Date:      2023-12-11 13:29:10
  * @Copyright  dragon
-->
<template>
  <a-drawer
      :title="'启用禁用'"
      width="500"
      :visible="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 30 }">
      <a-row>
        <a-form-item label="楼层" style="width: 100%" name="floor">
          <a-select style="width: 100%" v-model:value="form.floor">
            <a-select-option :value="1">1楼</a-select-option>
            <a-select-option :value="2">2楼</a-select-option>
          </a-select>
        </a-form-item>
      </a-row>
      <a-row>
        <a-form-item label="区域" style="width: 100%" name="position">
          <a-select v-model:value="form.position" placeholder="请选择区域">
            <a-select-option :value="11">左前</a-select-option>
            <a-select-option :value="12">左后</a-select-option>
            <a-select-option :value="21">中前</a-select-option>
            <a-select-option :value="22">中后</a-select-option>
            <a-select-option :value="31">右前</a-select-option>
            <a-select-option :value="32">右后</a-select-option>
          </a-select>
        </a-form-item>
      </a-row>
      <a-row>
        <a-form-item label="是否启用" style="width: 100%" name="status">
          <a-switch
              v-model:checked="form.status"
              checked-children="启用"
              un-checked-children="禁用"
              @change="changesSheduledPublishFlag"
          />
        </a-form-item>
      </a-row>

    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-drawer>
</template>
<script setup>
import {reactive, ref, nextTick} from 'vue';
import _ from 'lodash';
import {message} from 'ant-design-vue';
import {SmartLoading} from '/@/components/framework/smart-loading';
import {rolandSeaApi} from '/@/api/business/roland-sea/roland-sea-api';
import {smartSentry} from '/@/lib/smart-sentry';

// ------------------------ 事件 ------------------------

const emits = defineEmits(['reloadList']);

function changesSheduledPublishFlag(checked) {
  console.log(checked)
}

// ------------------------ 显示与隐藏 ------------------------
// 是否显示
const visibleFlag = ref(false);

function show(rowData) {
  Object.assign(form, formDefault);
  if (rowData && !_.isEmpty(rowData)) {
    Object.assign(form, rowData);
  }
  visibleFlag.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}

function onClose() {
  Object.assign(form, formDefault);
  visibleFlag.value = false;
}

// ------------------------ 表单 ------------------------

// 组件ref
const formRef = ref();

const formDefault = {
  floor: 1,
  position: undefined, //区域
  status: undefined, //是否启用
};

let form = reactive({...formDefault});

const rules = {
  floor: [{required: true, message: '楼层 必填'}],
  position: [{required: true, message: '区域 必填'}],
  status: [{required: true, message: '是否启用 必填'}],
};

// 点击确定，验证表单
async function onSubmit() {
  try {
    await formRef.value.validateFields();
    save();
  } catch (err) {
    message.error('参数验证错误，请仔细填写表单数据!');
  }
}

// 新建、编辑API
async function save() {
  SmartLoading.show();
  try {
    await rolandSeaApi.updateAreaStatus(form);
    message.success('操作成功');
    emits('reloadList');
    onClose();
  } catch (err) {
    smartSentry.captureError(err);
  } finally {
    SmartLoading.hide();
  }
}

defineExpose({
  show,
});
</script>
